package com.manage.service.impl;

import com.framework.base.dao.impl.BaseDaoImpl;
import com.manage.service.ChinaAreaService;
import com.model.TChina;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChinaAreaServiceImpl implements ChinaAreaService {
    @Resource(name="baseDaoImpl")
    BaseDaoImpl baseDao;

    @Override
    public TChina findById (Long id) {
        TChina model = null;
        if(id != null){
            List<Object> params = new ArrayList<Object>();
            StringBuilder hsql = new StringBuilder("from TChina c where 1=1 and c.isDeleted=0");
            params.add(id);
            hsql.append(" and c.id=?");
            try {
                model = (TChina) this.baseDao.getSingleByHsql(hsql.toString(),params);
            }catch (Exception e){
                System.out.println("查找ID="+id+"地区失败");
            }
        }
        return model;
    }

    @Override
    public List<TChina> findByParentId (Long parentId) {
        List<TChina> list = null;
        if(parentId != null){
            List<Object> params = new ArrayList<Object>();
            StringBuilder hsql = new StringBuilder("from TChina c where 1=1 and c.isDeleted=0");
            params.add(parentId);
            hsql.append(" and c.pid=? ");
            try {
                list =  this.baseDao.listByHql(hsql.toString(),params);
            }catch (Exception e){
                System.out.println("查找ID="+parentId+"地区失败");
            }
        }
        return list;
    }

    @Override
    public List<TChina> findByParentIdWithSelf (Long parentId) {
        List<TChina> list = null;
        if(parentId != null){
            StringBuilder hsql = new StringBuilder("from TChina c where 1=1 and c.isDeleted=0");
            hsql.append(" and c.pid=").append(parentId.toString());
            hsql.append(" or c.id=").append(parentId.toString());
            try {
                list =  this.baseDao.listByHql(hsql.toString(),null);
            }catch (Exception e){
                System.out.println("查找ID="+parentId+"地区失败");
            }
        }
        return list;
    }

    @Override
    public List<TChina> findAll () {
        List<TChina> list = null;
        StringBuilder hsql = new StringBuilder("from TChina c where 1=1 and c.isDeleted=0");
        try {
            list =  this.baseDao.listByHql(hsql.toString(),null);
        }catch (Exception e){
            System.out.println("查找所有地区失败");
        }


        return list;
    }

    @Override
    public TChina saveOrupdate (TChina tChina) {
        TChina model = null;
        if(tChina != null){
            try {
                model = (TChina) this.baseDao.saveOrupdate(tChina);
            }catch (Exception e){
                System.out.println("保存地区失败");
            }
        }
        return model;
    }

    @Override
    public void del (Long id) {
        List<Object> params=new ArrayList<Object>();
        if(id !=null){
            params.add(id);
            try{
                TChina tUnit=(TChina) this.baseDao.getSingleByHsql(" from TChina c where 1=1 and c.isDeleted=0 andc.id=?", params);
                tUnit.setIsDeleted(1);
                this.baseDao.saveOrupdate(tUnit);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
